/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.mhm.service.impl;

import me.zhengjie.modules.cspimp.mhm.domain.StorageAreaManage;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.mhm.repository.StorageAreaManageRepository;
import me.zhengjie.modules.cspimp.mhm.service.StorageAreaManageService;
import me.zhengjie.modules.cspimp.mhm.service.dto.StorageAreaManageDto;
import me.zhengjie.modules.cspimp.mhm.service.dto.StorageAreaManageQueryCriteria;
import me.zhengjie.modules.cspimp.mhm.service.mapstruct.StorageAreaManageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class StorageAreaManageServiceImpl implements StorageAreaManageService {

    private final StorageAreaManageRepository storageAreaManageRepository;
    private final StorageAreaManageMapper storageAreaManageMapper;

    @Override
    public Map<String,Object> queryAll(StorageAreaManageQueryCriteria criteria, Pageable pageable){
        Page<StorageAreaManage> page = storageAreaManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(storageAreaManageMapper::toDto));
    }

    @Override
    public List<StorageAreaManageDto> queryAll(StorageAreaManageQueryCriteria criteria){
        System.out.println("is:"+criteria.getId());
        return storageAreaManageMapper.toDto(storageAreaManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public StorageAreaManageDto findById(Integer id) {
        StorageAreaManage storageAreaManage = storageAreaManageRepository.findById(id).orElseGet(StorageAreaManage::new);
        ValidationUtil.isNull(storageAreaManage.getId(),"StorageAreaManage","id",id);
        return storageAreaManageMapper.toDto(storageAreaManage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StorageAreaManageDto create(StorageAreaManage resources) {
        return storageAreaManageMapper.toDto(storageAreaManageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(StorageAreaManage resources) {
        StorageAreaManage storageAreaManage = storageAreaManageRepository.findById(resources.getId()).orElseGet(StorageAreaManage::new);
        ValidationUtil.isNull( storageAreaManage.getId(),"StorageAreaManage","id",resources.getId());
        storageAreaManage.copy(resources);
        storageAreaManageRepository.save(storageAreaManage);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            storageAreaManageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<StorageAreaManageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StorageAreaManageDto storageAreaManage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", storageAreaManage.getCreateTime());
            map.put("更新时间", storageAreaManage.getUpdateTime());
            map.put("创建者", storageAreaManage.getCreateBy());
            map.put("更新者", storageAreaManage.getUpdateBy());
            map.put("储罐区名称", storageAreaManage.getStorageAreaName());
            map.put("储存物名称", storageAreaManage.getStorageThingName());
            map.put("储存量", storageAreaManage.getStorageNumbeer());
            map.put("防护堤面积", storageAreaManage.getProtectiveDikeArea());
            map.put("储罐数", storageAreaManage.getStorageJarNumber());
            map.put("储罐间距", storageAreaManage.getStorageJarSpace());
            map.put("储罐序号", storageAreaManage.getStorageJarSerialNumber());
            map.put("储罐名", storageAreaManage.getStorageJarName());
            map.put("储罐形式", storageAreaManage.getStorageJarModel());
            map.put("储罐材质", storageAreaManage.getStorageJarMaterial());
            map.put("储罐压力", storageAreaManage.getStorageJarPressure());
            map.put("审核周期", storageAreaManage.getAuditCycle());
            map.put("火灾等级", storageAreaManage.getFireClass());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}